package com.lgl.scsheader.sameCityChart.service;

import com.lgl.scsapi.base.dao.DaoSupport;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 其它订单图表service
 */

@Service("orderOtherChartService")
public class OrderOtherChartService {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/**
	 * 今年与去年订单总量折线图，数据格式放在前端处理，可以在一定程度上减轻服务器压力
	 * @param thisLastPd 统计参数，如统计的某年年份
	 * @return 本年与上一年订单总量数量
	 */
	public List<PageData> listThisLastIssueRobTotal(PageData thisLastPd){
		int thisYear = Tools.getYear(thisLastPd);
		StringBuffer issueCountSql = new StringBuffer(), robCountSql = new StringBuffer();
		//拼接12个月的统计SQL
		for (int g = 1; g < 13; g ++){
			issueCountSql.append("COUNT(IF(DATE_FORMAT(g.so_order_date, '%c') = '" + g + "', 1, NULL)) as '" + g + "',");
			robCountSql.append("COUNT(IF(DATE_FORMAT(g.so_rob_date, '%c') = '" + g + "', 1, NULL)) as '" + g + "',");
		}
		int lastYear = thisYear - 1;
		thisLastPd.put("issueMonthSql",issueCountSql);
		thisLastPd.put("robMonthSql",robCountSql);
		Tools.initDate(thisLastPd,thisYear,lastYear);
		List<PageData> thisLastList = (List<PageData>) dao.findForList("ServiceOrderMapper.listThisLastIssueRobTotal", thisLastPd);
		//如果没有数据或某一年没有数据，则重新添加进去
		if (thisLastList == null || thisLastList.size() < 4){
			putZeroToThisLastList(thisYear,thisLastList);
			putZeroToThisLastList(lastYear,thisLastList);
		}
		return thisLastList;
	}

	/**
	 * 给getThisLastYearOrderTotal中添加0的数据
	 * @param year 年份
	 * @param thisLastList 要添加的List对象
	 */
	private void putZeroToThisLastList(int year, List<PageData> thisLastList){
		if (thisLastList == null){
			thisLastList = new ArrayList<>(8);
		}
		String[] issueRobArr = {"i", "r"};
		issueRobFor: for (String ir : issueRobArr) {
			for (PageData p : thisLastList){
				//如果存在year年的ir值，则结束本次循环
				if (!p.containsValue(String.valueOf(year))){
					continue;
				}
				if (p.containsValue(ir)){
					continue issueRobFor;
				}
			}
			//如果不存在year年的ir值，则添加
			PageData tlPageData = new PageData(20);
			for(int i = 1; i < 13; i ++){
				tlPageData.put(i,0);
			}
			tlPageData.put("issueOrRobYear",year);
			tlPageData.put("issueOrRobFlag",ir);
			thisLastList.add(tlPageData);
		}
	}
	/**
	 * 获取订单各类型的总量
	 * @param typePd 统计参数，如统计的某年年份
	 * @return 订单各类型的总量
	 */
	public List<PageData> listTotalGroupType(PageData typePd){
		if (typePd != null && typePd.containsKey("thisYear")){
			int thisYear = Tools.getYear(typePd);
			Tools.initDate(typePd,thisYear,thisYear);
		}
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listTotalGroupType", typePd);
	}
	/**
	 * 获取每周几的订单总量
	 * @param weekPd 统计参数，如统计的某年年份
	 * @return 订单周1到周日的总量
	 */
	public List<PageData> listTotalGroupWeek(PageData weekPd){
		if (weekPd != null && weekPd.containsKey("thisYear")){
			int thisYear = Tools.getYear(weekPd);
			Tools.initDate(weekPd,thisYear,thisYear);
		}
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listTotalGroupWeek", weekPd);
	}
	/**
	 * 获取订单客户端来源的订单总量
	 * @param clientPd 统计参数，如统计的某年年份
	 * @return 订单客户端来源的订单总量
	 */
	public List<PageData> listTotalGroupClient(PageData clientPd){
		if (clientPd != null && clientPd.containsKey("thisYear")){
			int thisYear = Tools.getYear(clientPd);
			Tools.initDate(clientPd,thisYear,thisYear);
		}
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listTotalGroupClient", clientPd);
	}
	/**
	 * 获取订单各状态下的总量
	 * @param statusPd 统计参数，如统计的某年年份
	 * @return 订单各状态下的总量
	 */
	public List<PageData> listStatusTotal(PageData statusPd){
		if (statusPd != null && statusPd.containsKey("thisYear")){
			int thisYear = Tools.getYear(statusPd);
			Tools.initDate(statusPd,thisYear,thisYear);
		}
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listStatusTotal", statusPd);
	}
	/**
	 * 获取今年各类型订单总量数据集
	 * @param typePd 统计参数，如统计的某年年份
	 * @return 今年订单各类型订单总量数据集
	 */
	public List<PageData> listMonthOrderTypeTotal(PageData typePd){
		int thisYear = Tools.getYear(typePd);
		StringBuffer monthIfnullSql = new StringBuffer(), countMonthSql = new StringBuffer();
		//拼接12个月的统计SQL
		for (int g = 1; g < 13; g ++){
			String m = g < 10 ? "0" + g : String.valueOf(g);
			monthIfnullSql.append("IFNULL(g.`" + m + "`, 0) `" + g + "`,");
			countMonthSql.append("COUNT(IF(RIGHT(i.orderDate, 2) = '" + m + "', 1, NULL)) AS '" + m + "',");
		}
		typePd.put("monthIfnullSql",monthIfnullSql);
		typePd.put("countMonthSql",countMonthSql);
		Tools.initDate(typePd,thisYear,thisYear);
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listMonthOrderTypeTotal", typePd);
	}
	/**
	 * 获取每个月从下单到抢单、抢单到完成订单所花的平均天数
	 * @param dayPd 统计参数，如统计的某年年份
	 * @return 每个月从下单到抢单、抢单到完成订单所花的平均天数
	 */
	public List<PageData> listMonthIssueRobDay(PageData dayPd){
		int thisYear = Tools.getYear(dayPd);
		Tools.initDate(dayPd,thisYear,thisYear);
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listMonthIssueRobDay", dayPd);
	}
}